<?php
define('IN_PFT', true);
use \LaneWeChat\Core\WeChatOAuth;
//TODO::只有微信能打开
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')===false) {
    header('location:http://wx.12301.cc/html/error_weixin.html');
    exit;
}
define('DEFAULT_WECHAT_APPID', 'wxd72be21f7455640d');
//session_destroy();
include '/var/www/html/wx/wechat/iwx.php';
//echo $_SERVER['SERVER_NAME'];exit;
//TODO::获取粉丝信息
if (!isset($_GET['code']))
{
    WeChatOAuth::getCode('http://'.$_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
//    WeChatOAuth::getCode("http://503334.12301.cc{$_SERVER['REQUEST_URI']}?abc=efg", 100014);
}
if (isset($_GET['code']) && !empty($_GET['code'])) {
    $json = WeChatOAuth::getAccessTokenAndOpenId($_GET['code']);
    if ($json['errcode']) {
        header('location:error_weixin.html?msg=' . urlencode('获取用户身份出错'.$json['errcode']));
        exit;
    }
    $host = $_GET['state'] . '.12301.cc' . $_SERVER['REQUEST_URI'];
//    print_r($json);
//    echo $host;
//    exit;
//    header("location:");
//    print_r($json);exit;
    $_SESSION['openid'] = $json['openid'];
    $_SESSION['memberID'] = 94;
}
$creditMoney   = 100;//查询会员账户可用授信额度
//echo WECHAT_APPID;exit;
?>
<!DOCTYPE html class="no-js">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta charset="UTF-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="format-detection" content="telephone=no"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <link type="text/css" rel="stylesheet" href="http://wx.12301.cc/public/css/global.css" />
    <link rel="stylesheet" href="http://wx.12301.cc/public/css/pmakeorder.css"/>
    <title>账户充值</title>
    <link type="text/css" rel="stylesheet" href="http://wx.12301.cc/public/css/spaymode.css?v1.2" />
    <script type="text/javascript" src="http://wx.12301.cc/public/js/modernizr.js"></script>
</head>
<body>
<div id="fixedHead" class="fixedHead">
    <p class="con gtextoverflow">选择支付方式</p>
    <button id="fixedGoBack" class="fixedGoBack" ontouchend="javascript:history.go(-1)"><i class="fa fa-chevron-left"></i></button>
    <button id="fixMoreBtn" class="fixMoreBtn"><i class="fa fa-user"></i></button>
</div>
<div id="main" class="main">
    <div class="gbox infoBox">
        <div class="gline name">
            <input name="did" type="hidden" value="<?=$_SESSION['memberID']?>"/>
            <input name="aid" type="hidden" value="<?=PFT_MEMBER_ID?>"/>
            <div class="gcol_l gcol">充值金额</div>
            <div class="gcol_r gcol"><input class="gtextInp" type="number" name="money" id="money" placeholder="请输入充值金额（单位：元）"><span class="error errorred" style="display: none;">*请输入姓名</span></div>
        </div>
        <div class="gline mobile">
            <div class="gcol_l gcol">当前额度：</div>
            <div class="gcol_r gcol">
                <input class="gtextInp" type="number" readonly disabled
                       value="<?=$creditMoney?>">
            </div>
        </div>
        <div class="gline mobile">
            <div class="gcol_l gcol">OPENID：</div>
            <div class="gcol_r gcol">
                <input class="gtextInp" type="text"
                       value="<?=$_SESSION['openid']?>" />
            </div>
        </div>
    </div>
    <div class="paymodeW ">
        <p class="tit">请选择支付方式：</p>
        <ul class="con">
            <!--                目前只有票付通公众号可以使用微信支付-->
            <li class="payitem wxpay">
                <form id="wx_form">
                    <button class="submitBtn" type="button" ontouchend="callpay();">
                        <span class="img"></span><span class="t">微信支付</span><i class="fa fa-angle-right"></i>
                    </button>
                </form>
            </li>
            <li class="payitem alipay">
                <form id="pay_form" action="/wx/pay/alipay_v3.3/pay_iframe.html" method="post">
                    <input name="pay_action" type="hidden" value="2"/>
                    <input name="out_trade_no" value="<?=$out_trade_no?>" type="hidden"/>
                    <input name="subject" value="<?=$body?>" type="hidden"/>
                    <input name="total_fee" value="<?=$total_fee?>" type="hidden"/>
                    <input name="buy_id" value="<?=$buy_id?>" type="hidden"/>
                    <!--                        ontouchend="call_alipay();"-->
                    <button class="submitBtn" ontouchend="callpay('ap');" type="button">
                        <span class="img"></span><span class="t">支付宝支付</span><i class="fa fa-angle-right"></i>
                    </button>
                </form>
                <!--                    <iframe src="ifr.html" frameborder="0"></iframe>-->
            </li>
        </ul>
    </div>
</div>
<script type="text/javascript" src="http://wx.12301.cc/public/js/jquery-2.0.3.min.js"></script>
<script type="text/javascript" src="http://wx.12301.cc/public/js/plugins/touch-0.2.14.min.js"></script>
<script type="text/javascript" src="http://wx.12301.cc/public/js/global.js"></script>
<script>
    //调用微信JS api 支付
    function callpay(pay_type)
    {
        var money = $("input[name=money]").val();
        if (isNaN(money) || !money) {
            alert('金额输错了！');
            return false;
        }
        var post_data = {
            did : $("input[name=did]").val(),
            aid : $("input[name=aid]").val(),
            money : money,
            appid: '<?=WECHAT_APPID?>'
        };
        var callback;
        if (pay_type=='ap') {
            post_data['pay_type'] = 'alipay';
            $("input[name=total_fee]").val(money);
            callback = function(json){
                if (json.status=='ok') {
                    $("input[name=out_trade_no]").val(json.data.out_trade_no);
                    $("input[name=subject]").val(json.data.subject);
                    $("#pay_form").submit();
                } else {
                    alert(json.msg);
                }
            };
        } else {
            callback = function(json){
                if (json.status=='ok'){
                    WeixinJSBridge.invoke(
                        'getBrandWCPayRequest',
                        json.data,
                        function(res){
                            WeixinJSBridge.log(res.err_msg);
                            if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                                alert('支付成功');
//                                WeixinJSBridge.invoke('closeWindow',{},function(res){
//                                });
                            } else if(res.err_msg == "get_brand_wcpay_request:cancel") {
                                alert('您取消了支付，支付成功后订单才会生效哦~');
                            } else {
                                alert('支付失败，失败原因：'+res.err_msg);
                            }
                        }
                    );
                } else {
                    alert(json.msg);
                }
            };
        }
        $.post('http://www.12301.cc/shops/wepay/wx_recharge.php', post_data, function(json){
            callback(json);
        }, 'json');
    }
</script>
</body>